package com.xiaomi.controller;


import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Cpguige;

import com.xiaomi.service.CpguigeService;
import com.xiaomi.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CpguigeController
 * @Description TODO
 * @Author HP
 * @Date 2021/11/28 21:44
 */
@Controller
@RequestMapping("/cpguige")
public class CpguigeController {
        @Resource
        CpguigeService cpguigeService;

        @RequestMapping(value = "/chaxun", produces = "text/html;charset=UTF-8")
        @ResponseBody
        public String selectAll(@RequestParam(required = false) Integer page,
                                @RequestParam(required = false) Integer limit) {
            Map<String, Object> pars = new HashMap<>();

            if (page!=null && limit !=null){
                pars.put("offset",(page-1)*limit);
                pars.put("limit",limit);
            }
            List<Cpguige> list = cpguigeService.selectAll(pars);
            int count = cpguigeService.count(pars);
            Result result = new Result(1, "查询成功", count, list);

            return JSON.toJSONString(result);

        }

        @RequestMapping(value = "/add", produces = "text/html;charset=UTF-8")
        @ResponseBody
        public String add(Cpguige cpguige) {

            int n = cpguigeService.add(cpguige);
            if (n == 1) {
                return "添加成功";
            } else {
                return "添加失败";
            }

        }

        @RequestMapping(value = "/del", produces = "text/html;charset=UTF-8")
        @ResponseBody
        public String del(int id) {

            int n = cpguigeService.del(id);
            if (n == 1) {
                return "删除成功";
            } else {
                return "删除失败";
            }
        }

    @RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(Cpguige cpguige) {

        int n = cpguigeService.update(cpguige);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

}

